<template>
  <div class="app-container">
    <el-form ref="filterForm" :inline="true" :model="listQuery" class="filter-container">
      <el-form-item prop="id">
        <el-input v-model="listQuery.id" size="small" placeholder="ID" clearable style="width: 100px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="meetingTitle">
        <el-input v-model="listQuery.meetingTitle" size="small" placeholder="标题" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>

      <el-form-item prop="type">
        <el-select v-model="listQuery.type" size="small" placeholder="类型" clearable @change="handleFilter">
          <el-option v-for="item in meetingTypeOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="meetingType">
        <el-select v-model="listQuery.meetingType" size="small" placeholder="参会类型" clearable @change="handleFilter">
          <el-option v-for="item in joinMeetingTypeOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="productId">
        <el-select v-model="listQuery.productId" size="small" placeholder="相关产品" clearable @change="handleFilter">
          <el-option v-for="item in productsOptions" :key="item.id" :label="item.brand" :value="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item prop="provinceCode">
        <el-select
          v-model="listQuery.provinceCode"
          filterable
          clearable
          size="small"
          placeholder="请选择省份"
          style="width:100%"
          @change="changeProvince"
        >
          <el-option
            v-for="item in provinceOptions"
            :key="item.id"
            :label="item.province_name"
            :value="item.province_code"
          />
        </el-select>
      </el-form-item>

      <el-form-item prop="cityCode">
        <el-select
          v-model="listQuery.cityCode"
          filterable
          clearable
          size="small"
          placeholder="请选择城市"
          style="width:100%"
        >
          <el-option
            v-for="item in cityOptions"
            :key="item.id"
            :label="item.city_name"
            :value="item.city_code"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="isonline">
        <el-select v-model="listQuery.isonline" size="small" placeholder="是否上线" clearable style="width: 110px" @change="handleFilter">
          <el-option v-for="item in onlineOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="meetingStatus">
        <el-select v-model="listQuery.meetingStatus" size="small" placeholder="状态" clearable style="width: 110px" @change="handleFilter">
          <el-option v-for="item in meetingStatusOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >筛选</el-button>
        <el-button
          size="small"
          @click="resetFilter"
        >重置</el-button>
        <el-button
          size="small"
          type="success"
          icon="el-icon-plus"
          @click="toMeetingNew"
        >新建会议</el-button>
        <el-divider direction="vertical" />
        <el-button
          size="small"
          icon="el-icon-download"
          :loading="exportMeetingDataLoading"
          :disabled="exportMeetingDataLoading"
          @click="exportMeetingData"
        >导出会议主数据</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      stripe
      fit
      highlight-current-row
    >
      <el-table-column type="expand">
        <template slot-scope="scope">
          <div class="table-expand-row">
            直播链接：
            {{ scope.row.livebroadcasturl }}
          </div>
          <div class="table-expand-row">
            省份城市：
            {{ scope.row.province }} {{ scope.row.city }}
          </div>
          <div class="table-expand-row">
            召开地点：
            {{ scope.row.location }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="ID" width="80">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="标题">
        <template slot-scope="scope">
          {{ scope.row.meetingtitle }}
        </template>
      </el-table-column>
      <el-table-column label="封面" width="150" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.coverurl" alt="" width="100%">
        </template>
      </el-table-column>
      <el-table-column label="召开时间" width="160" align="center">
        <template slot-scope="scope">
          {{ scope.row.begintime }}
        </template>
      </el-table-column>
      <el-table-column label="结束时间" width="160" align="center">
        <template slot-scope="scope">
          {{ scope.row.endtime }}
        </template>
      </el-table-column>
      <el-table-column label="参会类型" width="100" align="center">
        <template slot-scope="scope">
          <el-tag
            effect="plain"
            size="mini"
            type="success"
          >{{ scope.row.meetingTypeName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="直播">
        <template slot-scope="scope">
          <router-link
            :to="`/live-broadcasts/edit/${scope.row.livebroadcastid}`"
          >{{ scope.row.livebroadcasttitle }}</router-link>
        </template>
      </el-table-column>
      <el-table-column label="类型" width="160" align="center">
        <template slot-scope="scope">
          <el-tag
            effect="plain"
            size="mini"
          >{{ scope.row.typeName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="上线" width="60" align="center">
        <template slot-scope="scope">
          <i v-if="scope.row.isonline === 1" class="el-icon-check text-success" />
          <i v-if="scope.row.isonline === 0" class="el-icon-close text-warning" />
        </template>
      </el-table-column>
      <el-table-column label="可分享" width="80" align="center">
        <template slot-scope="scope">
          <i v-if="scope.row.isshare === 1" class="el-icon-check text-success" />
          <i v-if="scope.row.isshare === 0" class="el-icon-close text-warning" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="状态" width="80" align="center">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.meetingstatus === 0"
            effect="plain"
            size="mini"
          >未开始</el-tag>
          <el-tag
            v-else-if="scope.row.meetingstatus === 1"
            effect="plain"
            size="mini"
          >未开放预约</el-tag>
          <el-tag
            v-else-if="scope.row.meetingstatus === 2"
            effect="plain"
            size="mini"
          >已开始</el-tag>
          <el-tag
            v-else-if="scope.row.meetingstatus === 3"
            effect="plain"
            size="mini"
          >已结束</el-tag>
          <el-tag
            v-else
            effect="plain"
            size="mini"
            type="info"
          >暂无</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-dropdown trigger="click">
            <el-button
              size="small"
              plain
            >操作 <i class="el-icon-arrow-down el-icon--right" /></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                @click.native="toMeetingShow(scope.row.id)"
              >会议详情</el-dropdown-item>
              <el-dropdown-item
                @click.native="toMeetingEdit(scope.row.id)"
              >编辑会议</el-dropdown-item>
              <el-dropdown-item
                divided
                @click.native="toMeetingInvite(scope.row.id)"
              >参会邀请</el-dropdown-item>
              <el-dropdown-item
                @click.native="toMeetingApply(scope.row.id)"
              >预约名单</el-dropdown-item>
              <el-dropdown-item
                @click.native="toMeetingSignin(scope.row.id)"
              >会议签到</el-dropdown-item>
              <el-dropdown-item
                @click.native="toMeetingSchedule(scope.row.id)"
              >会议日程</el-dropdown-item>
              <el-dropdown-item
                @click.native="toMeetingSurveys(scope.row.id)"
              >会后调研</el-dropdown-item>
              <el-dropdown-item
                @click.native="toMeetingVotes(scope.row.id)"
              >会议互动</el-dropdown-item>
              <el-dropdown-item
                @click.native="toMeetingNotes(scope.row.id)"
              >小纸条</el-dropdown-item>
              <!-- <el-dropdown-item
                divided
                @click.native="removeMeeting(scope.row.id)"
              >
                <span class="text-danger">删除会议</span>
              </el-dropdown-item> -->
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="fetchData" />
  </div>
</template>

<script>
import {
  getProvince,
  getCities,
  getProducts
} from '@/api/baseData'
import {
  getMeetingsList,
  removeMeeting,
  getJoinMeetingType,
  getMeetingType,
  getMeetingStatus
} from '@/api/meetings'
import {
  exportMeetingData
} from '@/api/statements'
import Pagination from '@/components/Pagination'
import {
  blobExport,
  formatQuery
} from '@/utils'

export default {
  name: 'MeetingsList',
  components: {
    Pagination
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        id: formatQuery(this.$route.query.id, false),
        meetingTitle: formatQuery(this.$route.query.meetingTitle, false),
        type: formatQuery(this.$route.query.type, true),
        meetingType: formatQuery(this.$route.query.meetingType, true),
        tumorType: formatQuery(this.$route.query.tumorType, true),
        productId: formatQuery(this.$route.query.productId, true),
        provinceCode: formatQuery(this.$route.query.provinceCode, false),
        cityCode: formatQuery(this.$route.query.cityCode, false),
        isonline: formatQuery(this.$route.query.isonline, true),
        meetingStatus: formatQuery(this.$route.query.meetingStatus, true),
        page: 1,
        size: 30
      },
      onlineOptions: [
        {
          name: '是',
          id: 1
        },
        {
          name: '否',
          id: 0
        }
      ],
      joinMeetingTypeOptions: [],
      meetingStatusOptions: [],
      meetingTypeOptions: [],
      tumorTypeOptions: [],
      productsOptions: [],
      provinceOptions: [],
      cityOptions: [],
      exportMeetingDataLoading: false
    }
  },
  // watch: {
  //   $route: {
  //     handler(route) {
  //       if (!this._.isNil(route.query.page) &&
  //         !this._.isEmpty(route.query.page)
  //       ) {
  //         this.listQuery.page = parseInt(route.query.page)
  //       } else {
  //         this.listQuery.page = 1
  //       }
  //     },
  //     immediate: true
  //   }
  // },
  created() {
    if (!this._.isNil(this.$route.query.page) &&
      !this._.isEmpty(this.$route.query.page)
    ) {
      this.listQuery.page = parseInt(this.$route.query.page)
    } else {
      this.listQuery.page = 1
    }
    this.fetchJoinMeetingType()
    this.fetchMeetingType()
    this.fetchMeetingStatus()
    this.fetchtProducts()
    this.fetchProvince()
    this.fetchData()

    const provinceCode = this.$route.query.provinceCode
    if (!this._.isEmpty(provinceCode)) {
      this.fetchCities(provinceCode)
    }
  },
  methods: {
    fetchJoinMeetingType() {
      getJoinMeetingType().then(response => {
        // console.log(response)
        this.joinMeetingTypeOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchMeetingType() {
      getMeetingType().then(response => {
        // console.log(response)
        this.meetingTypeOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchMeetingStatus() {
      getMeetingStatus().then(response => {
        // console.log(response)
        this.meetingStatusOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchtProducts() {
      getProducts().then(response => {
        // console.log(response)
        this.productsOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchProvince() {
      getProvince().then(response => {
        // console.log(response)
        this.provinceOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchCities(code) {
      getCities({ provinceCode: code }).then(response => {
        // console.log(response)
        this.cityOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchData() {
      this.listLoading = true
      const query = {
        ...this.$route.query,
        ...this.listQuery
      }
      this.$router.push({
        query
      }).catch(err => {
        console.log(err)
      })
      getMeetingsList(this.listQuery).then(response => {
        // console.log(response)
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      }).catch(err => {
        console.log(err)
      })
    },
    changeProvince(province) {
      this.listQuery.cityCode = ''
      this.cityOptions = []
      if (!this._.isEmpty(province)) {
        this.fetchCities(province)
      }
    },
    exportMeetingData() {
      this.exportMeetingDataLoading = true
      exportMeetingData().then(response => {
        // console.log(response)
        blobExport(response, '会议主数据.xlsx')

        this.$message({
          type: 'success',
          message: '导出成功！',
          duration: 5000
        })
        this.exportMeetingDataLoading = false
      }).catch(err => {
        console.log(err)
        this.exportMeetingDataLoading = false
      })
    },
    removeMeeting(id) {
      this.$confirm('此操作将永久删除该会议, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeMeeting({ meetingId: id }).then(response => {
          // console.log(response)
          this.fetchData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(err => {
          console.log(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.list = []
      const provinceCode = this.listQuery.provinceCode
      if (!this._.isEmpty(provinceCode)) {
        this.fetchCities(provinceCode)
      } else {
        this.cityOptions = []
      }
      this.fetchData()
    },
    resetFilter() {
      this.$refs.filterForm.resetFields()
      for (const i in this.listQuery) {
        if (i !== 'size') {
          this.listQuery[i] = undefined
        }
      }
      // console.log(this.listQuery)
      this.$router.push({
        query: {
          page: 1
        }
      }).catch(err => {
        console.log(err)
      })
      this.handleFilter()
    },
    toMeetingNew() {
      this.$router.push('/meetings/new')
    },
    toMeetingShow(id) {
      this.$router.push(`/meetings/list/${id}?type=invite`)
    },
    toMeetingEdit(id) {
      this.$router.push(`/meetings/edit/${id}`)
    },
    toMeetingInvite(id) {
      this.$router.push(`/meetings/list/${id}?type=invite`)
    },
    toMeetingApply(id) {
      this.$router.push(`/meetings/list/${id}?type=apply`)
    },
    toMeetingSignin(id) {
      this.$router.push(`/meetings/list/${id}?type=signin`)
    },
    toMeetingSchedule(id) {
      this.$router.push(`/meetings/list/${id}?type=schedule`)
    },
    toMeetingSurveys(id) {
      this.$router.push(`/meetings/list/${id}?type=surveys`)
    },
    toMeetingVotes(id) {
      this.$router.push(`/meetings/list/${id}?type=votes`)
    },
    toMeetingNotes(id) {
      this.$router.push(`/meetings/list/${id}/notes`)
    }
  }
}
</script>
